package com.ctshk.rpc.starvip.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.HttpUtils;
import com.ctshk.rpc.starvip.dto.start_vip.member.register.RegisterLoyMembershipCard;
import com.ctshk.rpc.starvip.dto.start_vip.member.register.RegisterMember;
import com.ctshk.rpc.starvip.dto.start_vip.member.register.RegisterMemberOperation;
import com.ctshk.rpc.starvip.dto.start_vip.user.*;
import com.ctshk.rpc.starvip.factory.member.MemberRegisterFactory;
import com.ctshk.rpc.starvip.factory.user.UserCreateOrUpdateReqFactory;
import com.ctshk.rpc.starvip.factory.user.UserInfoChangeNotifyReqFactory;
import com.ctshk.rpc.starvip.factory.user.UserInfoReqFactory;
import com.ctshk.rpc.starvip.req.member.MemberRegisterReq;
import com.ctshk.rpc.starvip.req.user.StarVipUserCreateOpUpdateReq;
import com.ctshk.rpc.starvip.req.user.UserInfoChangeNotifyReq;
import com.ctshk.rpc.starvip.req.user.UserInfoQueryReq;
import com.ctshk.rpc.starvip.service.IStarVipUserService;
import com.ctshk.rpc.starvip.util.IBMMqUtil;
import com.ctshk.rpc.starvip.util.JaxbUtil;
import com.ctshk.rpc.starvip.util.ReqUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @BelongsProject: ctshk-cloud
 * @BelongsPackage: com.ctshk.rpc.starvip.service.impl
 * @Author: hef
 * @CreateTime: 2021-04-12 10:56
 * @Description: 描述
 */
@DubboService
@Slf4j
public class StarVipUserServiceImpl implements IStarVipUserService {

    @Override
    public Result<Contact> userInfoQuery(UserInfoQueryReq req) {
        UserInfoReqFactory userInfoReqFactory = new UserInfoReqFactory(req);
        // 本地测试打开注释 start
        /*String result = null;
        JaxbUtil jaxbUtil = new JaxbUtil(userInfoReqFactory.produce().getClass());
        String sendMessage = jaxbUtil.toXml(userInfoReqFactory.produce(), "UTF-8");
        System.out.println(sendMessage);
        try {
            Map<String, String> header = new HashMap<>();
            header.put("Content-Type","application/json");
            result = HttpUtils.postRequest("http://47.115.21.70:9000/app/user/vip/testXml",sendMessage,header, response -> {
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);*/
        // 本地测试打开注释 end
        String result = ReqUtil.sendReq(userInfoReqFactory);
        JaxbUtil jaxbUtil = new JaxbUtil(UserInfo.class);
        UserInfo userInfo = jaxbUtil.<UserInfo>fromXml(result);
        Contact contact = null;
        if (Objects.nonNull(userInfo)
                && Objects.nonNull(userInfo.getListOfContact())
                && CollectionUtils.isNotEmpty(userInfo.getListOfContact().getContact())) {
            contact = userInfo.getListOfContact().getContact().get(0);
        }
        return Result.success(contact);
    }

    @Override
    public Result<StarVipUserCreateOpUpdateReq> createOrUpdate(StarVipUserCreateOpUpdateReq req) {
        UserCreateOrUpdateReqFactory userCreateOrUpdateReqFactory = new UserCreateOrUpdateReqFactory(req);
        // 本地测试打开注释 start
        /*String result = null;
        JaxbUtil jaxbUtil = new JaxbUtil(userCreateOrUpdateReqFactory.produce().getClass());
        String sendMessage = jaxbUtil.toXml(userCreateOrUpdateReqFactory.produce(), "UTF-8");
        System.out.println(sendMessage);
        try {
            Map<String, String> header = new HashMap<>();
            header.put("Content-Type","application/json");
            result = HttpUtils.postRequest("http://47.115.21.70:9000/app/user/vip/testXml",sendMessage,header, response -> {
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);*/
        // 本地测试打开注释 end
        String result = ReqUtil.sendReq(userCreateOrUpdateReqFactory);
        //返回结构与Request结构相同
        StarVipCreateOrUpdateDTO dto = ReqUtil.fromXml(StarVipCreateOrUpdateDTO.class, result);
        if (Objects.nonNull(dto) && Objects.nonNull(dto.getUpsertContact()) && Objects.nonNull(dto.getUpsertContact().getContact())) {
            return Result.success(
                    EntityUtil.copy(dto.getUpsertContact().getContact(),StarVipUserCreateOpUpdateReq.class)
            );
        }
        if (Objects.nonNull(dto.getErrorInfo()))
            throw new BusinessException(SystemError.STAR_VIP_20000, dto.getErrorInfo().getErrorMessage());
        return Result.failed(SystemError.SYS_437);
    }

    @Override
    public Result userInfoChangeNotify(UserInfoChangeNotifyReq req) {
        UserInfoChangeNotifyReqFactory reqFactory = new UserInfoChangeNotifyReqFactory(req);
        ReqUtil.sendReq(reqFactory);
        return Result.success();
    }

    @Override
    public String testXml(String xml){
        String str = null;
        try {
            str = IBMMqUtil.sendTest(xml);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(SystemError.STAR_VIP_20000, e.getMessage());
        }
        return str;
    }

    @Override
    public Result<MemberRegisterReq> register(MemberRegisterReq req) {
        MemberRegisterFactory factory = new MemberRegisterFactory(req);
        // 本地测试打开注释 start
        /*String result = null;
        JaxbUtil jaxbUtil = new JaxbUtil(factory.produce().getClass());
        String sendMessage = jaxbUtil.toXml(factory.produce(), "UTF-8");
        System.out.println(sendMessage);
        try {
            Map<String, String> header = new HashMap<>();
            header.put("Content-Type","application/json");
            result = HttpUtils.postRequest("http://47.115.21.70:9000/app/user/vip/testXml",sendMessage,header, response -> {
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);*/
        // 本地测试打开注释 end
        String result = ReqUtil.sendReq(factory);
        //返回结构与Request结构相同
        RegisterMemberOperation dto = ReqUtil.fromXml(RegisterMemberOperation.class, result);
        if (Objects.nonNull(dto.getErrorInfo())) {
            throw new BusinessException(SystemError.STAR_VIP_20000, dto.getErrorInfo().getErrorMessage());
        }
        try {
            RegisterMember registerMember = dto.getListOfMembers().getMember().get(0);
            RegisterLoyMembershipCard registerLoyMembershipCard = registerMember
                    .getListOfLoyMembershipCard().getLoyMembershipCard().get(0);
            req.setCardNumber(registerLoyMembershipCard.getCardNumber());
            req.setStartDate(registerLoyMembershipCard.getStartDate());
            req.setId(registerMember.getId());
            return Result.success(req);
        } catch (Exception e) {
            log.error("注册失败,原因: ",e);
            throw new BusinessException(SystemError.STAR_VIP_20000,"注册星赏会个人会员失败");
        }
    }


}
